<?php
/**
 * Created by PhpStorm.
 * User: xiangpeng
 * Date: 2017/1/5
 * Time: 20:57
 */

namespace Common\Model;


class UserAddessModel extends BaseModel
{
    //主键
    protected  $pk  = 'adid';
    //要操作那张表
    protected  $tableName =  'myaddess';
    //自动验证
    protected $_validate   =  [
        ['name','require','请输入收货人名字',1,3],
        ['addess','require','请填写收货地址',1,3],
        ['postcodes','require','请输入邮编',1,3],
        ['city','require','请选择城市名称',1,3],
        ['phone','require','请输入手机号',1,3],

    ];

    //地址添加方法
    public  function store($data){
        //执行验证
        if(!$this->create($data)){
            return ['status'=>'faild','message'=>$this->getError()];die;
        }
        //先查询数据库中是否存在，
        $oldData  = $this->where("uid={$data['uid']}")->select();
        //dd($oldData);
        if($oldData){
            foreach($oldData as $k=>$v){
                    //存在就将数据库中的status改为0
                    $this->where("adid={$v['adid']}")->setField('status',0);
            }
        }
        //执行添加
        $this->add($data);
        return ['status'=>'success','message'=>'添加成功'];
    }

    //地址修改方法
    public  function edit($data){
        //执行验证
        if(!$this->create($data)){
            return ['status'=>'faild','message'=>$this->getError()];die;
        }
        //执行添加
        $this->where("adid={$data['adid']}")->save($data);
        return ['status'=>'success','message'=>'修改成功'];
    }

}